package com.zwj.common.utils.treeNode;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class MenuTree {
	private List<Menu> menuList = new ArrayList<>();

	//建立树形结构
	public List<Menu> builTree() {
		List<Menu> treeMenus = new ArrayList<Menu>();
		for (Menu menuNode : getRootNode()) {
			menuNode = buildChilTree(menuNode);
			treeMenus.add(menuNode);
		}
		return treeMenus;
	}

	//递归，建立子树形结构
	public Menu buildChilTree(Menu pNode) {
		List<Menu> chilMenus = new ArrayList<Menu>();
		for (Menu menuNode : menuList) {
			if (menuNode.getParentId().equals(pNode.getId())) {
				chilMenus.add(buildChilTree(menuNode));
			}
		}
		pNode.setChildren(chilMenus);
		return pNode;
	}

	//获取根节点(获取所有的父节点)
	public List<Menu> getRootNode() {
		List<Menu> rootMenuLists = new ArrayList<Menu>();
		for (Menu menuNode : menuList) {
			if (menuNode.getParentId().equals("0")) {
				rootMenuLists.add(menuNode);
			}
		}
		return rootMenuLists;
	}
}

